/**
 * 
 */
package com.fwzc.giuseppe.activity;

import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import com.fwzc.giuseppe.R;
import com.fwzc.giuseppe.adapter.AccountsAdapter;
import com.fwzc.giuseppe.base.BaseActivity;
import com.fwzc.giuseppe.dao.UserKeyDao;
import com.fwzc.giuseppe.entity.UserKey;
import com.fwzc.giuseppe.util.BitmapUtils;

/**
 * 
 * 类名称：SwitchAccountsActivity 类描述： 创建人：何琳 创建时间：2015年8月14日 下午3:37:36
 * 
 * @version
 * 
 */
public class SwitchAccountsActivity extends BaseActivity implements
		OnClickListener {

	private ListView listview;
	private AccountsAdapter mAdapter;
	private UserKeyDao dao;
	private ArrayList<UserKey> datas;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_switching_accounts);

		initBar();
		initheadView();
		initView();
		initData();
	}

	private void initheadView() {
		actionbar_side_name.setText("切换账号");
		iv_actionbar_side_left.setVisibility(View.VISIBLE);
		tv_actionbar_side_right.setVisibility(View.VISIBLE);
		tv_actionbar_side_right.setTextColor(getResources().getColor(
				R.color.black_56));
		tv_actionbar_side_right.setText("添加账户");

	}

	private void initView() {
		dao = new UserKeyDao(this);
		datas = dao.findUserKey();
		listview = (ListView) findViewById(R.id.accounts_list);
		mAdapter = new AccountsAdapter(this,datas);
	}

	private void initData() {
		listview.setAdapter(mAdapter);
		iv_actionbar_side_left.setOnClickListener(this);
		tv_actionbar_side_right.setOnClickListener(this);
		
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				SharedPreferences sp = getSharedPreferences("user", Activity.MODE_PRIVATE);
				Editor editor = sp.edit();
				editor.putString("userId", datas.get(position).userId);
				editor.putString("userIdentityNumber", datas.get(position).userIdentityNumber);
				editor.commit();
				setResult(RESULT_OK);
				finish();
			}
		});
	}

	@Override
	public void onClick(View v) {
		if (v == iv_actionbar_side_left) {
			finish();
		} else if (v == tv_actionbar_side_right) {
			Intent intent = new Intent(SwitchAccountsActivity.this,
					AddUserActivity.class);
			startActivityForResult(intent, 1);
		}
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK&&requestCode==1) {
			UserKey userKey = new UserKey();
			userKey.userId = data.getStringExtra("userId");
			userKey.userIdentityNumber = data.getStringExtra("userIdentityNumber");
			userKey.name = data.getStringExtra("name");
			userKey.headPortrait = data.getStringExtra("headPortrait");
			datas.add(userKey);
			mAdapter.notifyDataSetChanged();
		}
	}
}
